package java;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Question15 {
    public static void main(String[] args) {
        /**
         * 核心：排序后，利用双指针求和解法，同时去重。
         */
        int[] nums = {0,0,0};
        List<List<Integer>> resList = threeSum(nums);
        List<List<Integer>> resList1 = threeSum1(nums);
        // 这是别人的
        List<List<Integer>> resList2 = threeSum2(nums);
        for (List<Integer> item: resList){
            System.out.println(item.toString());
        }
        System.out.println("=================");
        for (List<Integer> item: resList1){
            System.out.println(item.toString());
        }
        System.out.println("=================");
        for (List<Integer> item: resList2){
            System.out.println(item.toString());
        }


    }

    public static List<List<Integer>> threeSum2(int[] nums) {
        List<List<Integer>> ans = new ArrayList();
        int len = nums.length;
        if(nums == null || len < 3){
            return ans;
        }
        Arrays.sort(nums); // 排序
        for (int i = 0; i < len ; i++) {
            if(nums[i] > 0){
                break; // 如果当前数字大于0，则三数之和一定大于0，所以结束循环
            }
            if(i > 0 && nums[i] == nums[i-1]){
                continue; // 去重
            }
            int L = i + 1;
            int R = len - 1;
            while(L < R){
                int sum = nums[i] + nums[L] + nums[R];
                if(sum == 0){
                    ans.add(Arrays.asList(nums[i],nums[L],nums[R]));
                    while (L < R && nums[L] == nums[L+1]){
                        L++; // 去重
                    }
                    while (L < R && nums[R] == nums[R-1]){
                        R--; // 去重
                    }
                    L++;
                    R--;
                }
                else if (sum < 0){
                    L++;
                }
                else if (sum > 0){
                    R--;
                }
            }
        }
        return ans;
    }


    private static List<List<Integer>> threeSum1(int[] nums) {
        List<List<Integer>> resList = new LinkedList<>();
        if (nums == null || nums.length < 3){
            return resList;
        }
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 2 ; i ++){
            if (nums[i] > 0){
                break;
            }
            if (i > 0 && nums[i] == nums[i - 1]){
                continue;
            }
            findThree1(resList,nums,i);
        }
        return resList;
    }

    private static void findThree1(List<List<Integer>> resList, int[] nums, int index) {
        if (nums == null || nums.length < 3){
            return ;
        }
        int left = index + 1;
        int right = nums.length - 1;
        while (left < right){
            if (nums[left] + nums[right] + nums[index] < 0){
                left ++;
            }else if (nums[left] + nums[right] + nums[index] > 0){
                right --;
            }else {
                while (left < right && nums[left] == nums[left + 1]){
                    left++; // 去重
                }
                while (left < right && nums[right] == nums[right - 1]){
                    right--; // 去重
                }
                List<Integer> oneRes = new LinkedList<>();
                oneRes.add(nums[index]);
                oneRes.add(nums[left]);
                oneRes.add(nums[right]);
                if (!resList.contains(oneRes)){
                    resList.add(oneRes);
                }
                left ++;
                right --;
            }
        }
    }

    public static List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> resList = new LinkedList<>();
        if (nums == null || nums.length < 3){
            return null;
        }
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 2 ; i ++){
            for (int j = i + 1; j < nums.length - 1;j ++){
                if (nums[i] + nums[j] + nums[j + 1] > 0 || nums[i] + nums[j] + nums[nums.length - 1] < 0){
                    continue;
                }
                int sum = nums[i] + nums[j];
                Integer three = findThree(nums,j + 1,sum);
                if (three != null){
                    List<Integer> oneRes = new LinkedList<>();
                    oneRes.add(nums[i]);
                    oneRes.add(nums[j]);
                    oneRes.add(nums[three]);
                    if (!resList.contains(oneRes)){
                        resList.add(oneRes);
                    }
                }
            }
        }
        return resList;
    }

    private static Integer findThree(int[] nums, int start, int sum) {
        int end = nums.length - 1;
        int center = (start + end)/2;
        while (start < end){
            if (sum + nums[center] < 0){
                start = center + 1;
            }else if (sum + nums[center] > 0){
                end = center - 1;
            }else {
                return center;
            }
            center = (start + end)/2;
        }
        if (sum + nums[center] == 0){
            return center;
        }
        return null;
    }
}
